package com.hmyuan.controller.back;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmyuan.domain.Article;
import com.hmyuan.domain.Article_category;
import com.hmyuan.domain.Category;
import com.hmyuan.domain.Sys_user;
import com.hmyuan.domain.Tag;
import com.hmyuan.service.ArticleService;
import com.hmyuan.service.CategoryService;
import com.hmyuan.service.TagService;
import com.hmyuan.util.PageUtil;
import com.hmyuan.util.UserUtil;

/**
 * 文章controller
 * @author hmyuan
 *
 */
@Controller
@RequestMapping("/back/article")
public class ArticleController {

	@Resource
	ArticleService articleService;
	@Resource
	CategoryService categoryService;
	@Resource
	TagService tagService;
	@Resource
	PageUtil pageUtil;
	/**
	 * 文章管理页面
	 */
	@RequestMapping("/articlePage")
	public String articlePage(HttpServletRequest request,Model model){
		return "/back/article/articlePage";
	}
	/**
	 * 分页查询文章列表
	 */
	@RequestMapping("/findarticleList")
	public String findarticleList(HttpServletRequest request,Model model,Integer pageNum,Article article){
		if(pageNum==null) pageNum=1;
		request.setAttribute("pageInfo", articleService.selectListByPage(article, pageNum, 10));
		return "/back/article/articlePageList";
	}
	/**
	 * 文章新增页面
	 */
	@RequestMapping("/articleAddPage")
	public String articleAddPage(HttpServletRequest request,Model model){
		request.setAttribute("categorys", categoryService.selectList(new Category()));
		request.setAttribute("tags", tagService.selectList(new Tag()));
		return "/back/article/articleAddPage";
	}
	/**
	 * 新增文章页面提交
	 */
	@ResponseBody
	@RequestMapping("/addarticle")
	public String addarticle(HttpServletRequest request,Model model,
			Article article,
			long category,
			Long[] tags,
			String[] tagsnew
	){
		/*获取当前用户*/
		Sys_user currentUser = UserUtil.getCurrentUser(request);
		/*文章初始化*/
		article.setUserid(currentUser.getId());
		article.setUseraccount(currentUser.getAccountname());
		article.setCreatetime(new Date());
		article.setClicknum(0l);
		articleService.insert(article);
		/*保存文章目录*/
		Article_category article_category = new Article_category();
		article_category.setArticleid(article.getId());
		article_category.setCategoryid(category);
		articleService.insert_article_category(article_category);
		/*保存文章标签*/
		if(tags!=null){
			articleService.insert_article_tags(article.getId(),tags);
		}
		if(tagsnew!=null){
			Long[] tagsnewid = new Long[tagsnew.length];
			for (int i=0;i<tagsnew.length;i++) {
				Tag tag = new Tag();
				tag.setName(tagsnew[i]);
				tag.setUseraccount(currentUser.getAccountname());
				tagService.insert(tag);
				tagsnewid[i] = tag.getId();
			}
			articleService.insert_article_tags(article.getId(),tagsnewid);
		}
		
		//更新页面左侧列表
		pageUtil.headPageListInitCategory();
		return "success";
	}
	/**
	 * 编辑文章信息页面
	 */
	@RequestMapping("/articleUpdatePage")
	public String articleUpdatePage(HttpServletRequest request,Model model,Long id){
		if(null==id) return "/article/articleAddPage";
		Article article = articleService.selectByPrimaryKey(id);
		request.setAttribute("article", article);
		request.setAttribute("checkcategory", categoryService.selectListByArticleid(id).get(0));
		request.setAttribute("checktags", tagService.selectListByArticleid(id));
		request.setAttribute("categorys", categoryService.selectList(new Category()));
		request.setAttribute("tags", tagService.selectList(new Tag()));
		return "/back/article/articleUpdatePage";
	}
	/**
	 * 编辑文章页面提交
	 */
	@ResponseBody
	@RequestMapping("/updatearticle")
	public String updatearticle(HttpServletRequest request,Model model,
			Article article,
			long category,
			Long[] tags,
			String[] tagsnew
	){
		/*获取当前用户*/
		Sys_user currentUser = UserUtil.getCurrentUser(request);
		if(null==article.getId()) return "fail";
		articleService.updateByPrimaryKeySelective(article);
		/*保存文章目录*/
		Article_category article_category = new Article_category();
		article_category.setArticleid(article.getId());
		article_category.setCategoryid(category);
		articleService.delete_article_categoryByArticleid(article.getId());
		articleService.insert_article_category(article_category);
		/*保存文章标签*/
		articleService.delete_article_tagsByArticleid(article.getId());
		if(tags!=null){
			articleService.insert_article_tags(article.getId(),tags);
		}
		if(tagsnew!=null){
			Long[] tagsnewid = new Long[tagsnew.length];
			for (int i=0;i<tagsnew.length;i++) {
				Tag tag = new Tag();
				tag.setName(tagsnew[i]);
				tag.setUseraccount(currentUser.getAccountname());
				tagService.insert(tag);
				tagsnewid[i] = tag.getId();
			}
			articleService.insert_article_tags(article.getId(),tagsnewid);
		}
		
		//更新页面左侧列表
		pageUtil.headPageListInitCategory();
		return "success";
	}
	/**
	 * 根据id删除文章
	 */
	@ResponseBody
	@RequestMapping("/deletearticleById")
	public String deletearticleById(HttpServletRequest request,Model model,Long id){
		if(null==id) return "fail";
		articleService.deletearticleById(id);
		
		//更新页面左侧列表
		pageUtil.headPageListInitCategory();
		return "success";
	}
}
